package com.example.zhengmingli

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class StudentAdapter(private var students: MutableList<Student>) :
    RecyclerView.Adapter<StudentAdapter.StudentViewHolder>() {

    class StudentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val studentIdTextView: TextView = itemView.findViewById(R.id.id_text)
        val studentNameTextView: TextView = itemView.findViewById(R.id.name_text)
        val studentGenderTextView: TextView = itemView.findViewById(R.id.gender_text)
        val studentClassNameTextView: TextView = itemView.findViewById(R.id.class_text)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.student_item, parent, false)
        return StudentViewHolder(view)
    }

    override fun onBindViewHolder(holder: StudentViewHolder, position: Int) {
        val student = students[position]
        holder.studentIdTextView.text = student.id
        holder.studentNameTextView.text = student.name
        holder.studentGenderTextView.text = student.gender
        holder.studentClassNameTextView.text = student.className
    }


    override fun getItemCount() = students.size

    fun addStudent(student: Student) {
        students.add(student)
        notifyItemInserted(students.size - 1)
    }
}
